{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import time\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-01</th>\n",
       "      <td>749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-02-01</th>\n",
       "      <td>659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-03-01</th>\n",
       "      <td>649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-04-01</th>\n",
       "      <td>724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-05-01</th>\n",
       "      <td>656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-06-01</th>\n",
       "      <td>719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-07-01</th>\n",
       "      <td>710</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-01</th>\n",
       "      <td>959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-01</th>\n",
       "      <td>728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-10-01</th>\n",
       "      <td>798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-11-01</th>\n",
       "      <td>856</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-01</th>\n",
       "      <td>990</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>855</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-01</th>\n",
       "      <td>644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-03-01</th>\n",
       "      <td>679</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-01</th>\n",
       "      <td>689</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-01</th>\n",
       "      <td>532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-06-01</th>\n",
       "      <td>722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-07-01</th>\n",
       "      <td>839</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-01</th>\n",
       "      <td>927</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-01</th>\n",
       "      <td>917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-10-01</th>\n",
       "      <td>544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-01</th>\n",
       "      <td>999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-01</th>\n",
       "      <td>649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-01</th>\n",
       "      <td>615</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-02-01</th>\n",
       "      <td>928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-01</th>\n",
       "      <td>826</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            销售额\n",
       "日期             \n",
       "2019-01-01  749\n",
       "2019-02-01  659\n",
       "2019-03-01  649\n",
       "2019-04-01  724\n",
       "2019-05-01  656\n",
       "2019-06-01  719\n",
       "2019-07-01  710\n",
       "2019-08-01  959\n",
       "2019-09-01  728\n",
       "2019-10-01  798\n",
       "2019-11-01  856\n",
       "2019-12-01  990\n",
       "2020-01-01  855\n",
       "2020-02-01  644\n",
       "2020-03-01  679\n",
       "2020-04-01  689\n",
       "2020-05-01  532\n",
       "2020-06-01  722\n",
       "2020-07-01  839\n",
       "2020-08-01  927\n",
       "2020-09-01  917\n",
       "2020-10-01  544\n",
       "2020-11-01  999\n",
       "2020-12-01  649\n",
       "2021-01-01  615\n",
       "2021-02-01  928\n",
       "2021-03-01  826"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入数据集\n",
    "df_raw = pd.read_excel('./data.xlsx',sheet_name='Sheet1')\n",
    "df_raw['日期'] = pd.to_datetime(df_raw['日期'])\n",
    "df_raw = df_raw.set_index('日期')\n",
    "df_raw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>环比</th>\n",
       "      <th>同比</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-01</th>\n",
       "      <td>749</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-02-01</th>\n",
       "      <td>659</td>\n",
       "      <td>-12.02%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-03-01</th>\n",
       "      <td>649</td>\n",
       "      <td>-1.52%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-04-01</th>\n",
       "      <td>724</td>\n",
       "      <td>11.56%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-05-01</th>\n",
       "      <td>656</td>\n",
       "      <td>-9.39%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-06-01</th>\n",
       "      <td>719</td>\n",
       "      <td>9.6%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-07-01</th>\n",
       "      <td>710</td>\n",
       "      <td>-1.25%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-01</th>\n",
       "      <td>959</td>\n",
       "      <td>35.07%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-01</th>\n",
       "      <td>728</td>\n",
       "      <td>-24.09%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-10-01</th>\n",
       "      <td>798</td>\n",
       "      <td>9.62%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-11-01</th>\n",
       "      <td>856</td>\n",
       "      <td>7.27%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-01</th>\n",
       "      <td>990</td>\n",
       "      <td>15.65%</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>855</td>\n",
       "      <td>-13.64%</td>\n",
       "      <td>14.15%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-01</th>\n",
       "      <td>644</td>\n",
       "      <td>-24.68%</td>\n",
       "      <td>-2.28%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-03-01</th>\n",
       "      <td>679</td>\n",
       "      <td>5.43%</td>\n",
       "      <td>4.62%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-01</th>\n",
       "      <td>689</td>\n",
       "      <td>1.47%</td>\n",
       "      <td>-4.83%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-01</th>\n",
       "      <td>532</td>\n",
       "      <td>-22.79%</td>\n",
       "      <td>-18.9%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-06-01</th>\n",
       "      <td>722</td>\n",
       "      <td>35.71%</td>\n",
       "      <td>0.42%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-07-01</th>\n",
       "      <td>839</td>\n",
       "      <td>16.2%</td>\n",
       "      <td>18.17%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-01</th>\n",
       "      <td>927</td>\n",
       "      <td>10.49%</td>\n",
       "      <td>-3.34%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-01</th>\n",
       "      <td>917</td>\n",
       "      <td>-1.08%</td>\n",
       "      <td>25.96%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-10-01</th>\n",
       "      <td>544</td>\n",
       "      <td>-40.68%</td>\n",
       "      <td>-31.83%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-01</th>\n",
       "      <td>999</td>\n",
       "      <td>83.64%</td>\n",
       "      <td>16.71%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-01</th>\n",
       "      <td>649</td>\n",
       "      <td>-35.04%</td>\n",
       "      <td>-34.44%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-01</th>\n",
       "      <td>615</td>\n",
       "      <td>-5.24%</td>\n",
       "      <td>-28.07%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-02-01</th>\n",
       "      <td>928</td>\n",
       "      <td>50.89%</td>\n",
       "      <td>44.1%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-01</th>\n",
       "      <td>826</td>\n",
       "      <td>-10.99%</td>\n",
       "      <td>21.65%</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            销售额       环比       同比\n",
       "日期                               \n",
       "2019-01-01  749                  \n",
       "2019-02-01  659  -12.02%         \n",
       "2019-03-01  649   -1.52%         \n",
       "2019-04-01  724   11.56%         \n",
       "2019-05-01  656   -9.39%         \n",
       "2019-06-01  719     9.6%         \n",
       "2019-07-01  710   -1.25%         \n",
       "2019-08-01  959   35.07%         \n",
       "2019-09-01  728  -24.09%         \n",
       "2019-10-01  798    9.62%         \n",
       "2019-11-01  856    7.27%         \n",
       "2019-12-01  990   15.65%         \n",
       "2020-01-01  855  -13.64%   14.15%\n",
       "2020-02-01  644  -24.68%   -2.28%\n",
       "2020-03-01  679    5.43%    4.62%\n",
       "2020-04-01  689    1.47%   -4.83%\n",
       "2020-05-01  532  -22.79%   -18.9%\n",
       "2020-06-01  722   35.71%    0.42%\n",
       "2020-07-01  839    16.2%   18.17%\n",
       "2020-08-01  927   10.49%   -3.34%\n",
       "2020-09-01  917   -1.08%   25.96%\n",
       "2020-10-01  544  -40.68%  -31.83%\n",
       "2020-11-01  999   83.64%   16.71%\n",
       "2020-12-01  649  -35.04%  -34.44%\n",
       "2021-01-01  615   -5.24%  -28.07%\n",
       "2021-02-01  928   50.89%    44.1%\n",
       "2021-03-01  826  -10.99%   21.65%"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pct_change() 函数，直接可以计算百分比对比（同比、环比）\n",
    "df_raw['环比'] = df_raw['销售额'].pct_change(periods=1)\n",
    "df_raw['同比'] = df_raw['销售额'].pct_change(periods=12)\n",
    "df_raw['环比'] = df_raw['环比'].apply(lambda x:str(round(x * 100,2)) + '%').str.replace('nan%','')\n",
    "df_raw['同比'] = df_raw['同比'].apply(lambda x:str(round(x * 100,2)) + '%').str.replace('nan%','')\n",
    "df_raw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>环比</th>\n",
       "      <th>同比</th>\n",
       "      <th>定基比</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-01</th>\n",
       "      <td>749</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>37.68%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-02-01</th>\n",
       "      <td>659</td>\n",
       "      <td>-12.02%</td>\n",
       "      <td></td>\n",
       "      <td>21.14%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-03-01</th>\n",
       "      <td>649</td>\n",
       "      <td>-1.52%</td>\n",
       "      <td></td>\n",
       "      <td>19.3%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-04-01</th>\n",
       "      <td>724</td>\n",
       "      <td>11.56%</td>\n",
       "      <td></td>\n",
       "      <td>33.09%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-05-01</th>\n",
       "      <td>656</td>\n",
       "      <td>-9.39%</td>\n",
       "      <td></td>\n",
       "      <td>20.59%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-06-01</th>\n",
       "      <td>719</td>\n",
       "      <td>9.6%</td>\n",
       "      <td></td>\n",
       "      <td>32.17%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-07-01</th>\n",
       "      <td>710</td>\n",
       "      <td>-1.25%</td>\n",
       "      <td></td>\n",
       "      <td>30.51%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-01</th>\n",
       "      <td>959</td>\n",
       "      <td>35.07%</td>\n",
       "      <td></td>\n",
       "      <td>76.29%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-01</th>\n",
       "      <td>728</td>\n",
       "      <td>-24.09%</td>\n",
       "      <td></td>\n",
       "      <td>33.82%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-10-01</th>\n",
       "      <td>798</td>\n",
       "      <td>9.62%</td>\n",
       "      <td></td>\n",
       "      <td>46.69%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-11-01</th>\n",
       "      <td>856</td>\n",
       "      <td>7.27%</td>\n",
       "      <td></td>\n",
       "      <td>57.35%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-01</th>\n",
       "      <td>990</td>\n",
       "      <td>15.65%</td>\n",
       "      <td></td>\n",
       "      <td>81.99%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>855</td>\n",
       "      <td>-13.64%</td>\n",
       "      <td>14.15%</td>\n",
       "      <td>57.17%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-01</th>\n",
       "      <td>644</td>\n",
       "      <td>-24.68%</td>\n",
       "      <td>-2.28%</td>\n",
       "      <td>18.38%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-03-01</th>\n",
       "      <td>679</td>\n",
       "      <td>5.43%</td>\n",
       "      <td>4.62%</td>\n",
       "      <td>24.82%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-01</th>\n",
       "      <td>689</td>\n",
       "      <td>1.47%</td>\n",
       "      <td>-4.83%</td>\n",
       "      <td>26.65%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-01</th>\n",
       "      <td>532</td>\n",
       "      <td>-22.79%</td>\n",
       "      <td>-18.9%</td>\n",
       "      <td>-2.21%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-06-01</th>\n",
       "      <td>722</td>\n",
       "      <td>35.71%</td>\n",
       "      <td>0.42%</td>\n",
       "      <td>32.72%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-07-01</th>\n",
       "      <td>839</td>\n",
       "      <td>16.2%</td>\n",
       "      <td>18.17%</td>\n",
       "      <td>54.23%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-01</th>\n",
       "      <td>927</td>\n",
       "      <td>10.49%</td>\n",
       "      <td>-3.34%</td>\n",
       "      <td>70.4%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-01</th>\n",
       "      <td>917</td>\n",
       "      <td>-1.08%</td>\n",
       "      <td>25.96%</td>\n",
       "      <td>68.57%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-10-01</th>\n",
       "      <td>544</td>\n",
       "      <td>-40.68%</td>\n",
       "      <td>-31.83%</td>\n",
       "      <td>0.0%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-01</th>\n",
       "      <td>999</td>\n",
       "      <td>83.64%</td>\n",
       "      <td>16.71%</td>\n",
       "      <td>83.64%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-01</th>\n",
       "      <td>649</td>\n",
       "      <td>-35.04%</td>\n",
       "      <td>-34.44%</td>\n",
       "      <td>19.3%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-01</th>\n",
       "      <td>615</td>\n",
       "      <td>-5.24%</td>\n",
       "      <td>-28.07%</td>\n",
       "      <td>13.05%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-02-01</th>\n",
       "      <td>928</td>\n",
       "      <td>50.89%</td>\n",
       "      <td>44.1%</td>\n",
       "      <td>70.59%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-01</th>\n",
       "      <td>826</td>\n",
       "      <td>-10.99%</td>\n",
       "      <td>21.65%</td>\n",
       "      <td>51.84%</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            销售额       环比       同比     定基比\n",
       "日期                                       \n",
       "2019-01-01  749                    37.68%\n",
       "2019-02-01  659  -12.02%           21.14%\n",
       "2019-03-01  649   -1.52%            19.3%\n",
       "2019-04-01  724   11.56%           33.09%\n",
       "2019-05-01  656   -9.39%           20.59%\n",
       "2019-06-01  719     9.6%           32.17%\n",
       "2019-07-01  710   -1.25%           30.51%\n",
       "2019-08-01  959   35.07%           76.29%\n",
       "2019-09-01  728  -24.09%           33.82%\n",
       "2019-10-01  798    9.62%           46.69%\n",
       "2019-11-01  856    7.27%           57.35%\n",
       "2019-12-01  990   15.65%           81.99%\n",
       "2020-01-01  855  -13.64%   14.15%  57.17%\n",
       "2020-02-01  644  -24.68%   -2.28%  18.38%\n",
       "2020-03-01  679    5.43%    4.62%  24.82%\n",
       "2020-04-01  689    1.47%   -4.83%  26.65%\n",
       "2020-05-01  532  -22.79%   -18.9%  -2.21%\n",
       "2020-06-01  722   35.71%    0.42%  32.72%\n",
       "2020-07-01  839    16.2%   18.17%  54.23%\n",
       "2020-08-01  927   10.49%   -3.34%   70.4%\n",
       "2020-09-01  917   -1.08%   25.96%  68.57%\n",
       "2020-10-01  544  -40.68%  -31.83%    0.0%\n",
       "2020-11-01  999   83.64%   16.71%  83.64%\n",
       "2020-12-01  649  -35.04%  -34.44%   19.3%\n",
       "2021-01-01  615   -5.24%  -28.07%  13.05%\n",
       "2021-02-01  928   50.89%    44.1%  70.59%\n",
       "2021-03-01  826  -10.99%   21.65%  51.84%"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算定基比（'2020-10-01'）\n",
    "fixed_str = '2020-10-01'\n",
    "# fixed_row = datetime.datetime.strptime(fixed_str, '%Y-%m-%d').date()\n",
    "fixed_num = df_raw.loc[fixed_str,'销售额']\n",
    "df_raw['定基比'] = df_raw['销售额'] / fixed_num - 1\n",
    "df_raw['定基比'] = df_raw['定基比'].apply(lambda x:str(round(x * 100,2)) + '%').str.replace('nan%','')\n",
    "df_raw"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
